package com.lx.idea.common.dto;

import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: jin
 * @CreateTime: 2019-11-04 15:44
 * @Description: 响应工具类
 */
public class ResponseUtil {

    /**
     * 将响应以json的方式返回给前台
     * @param response
     * @param data
     * @throws IOException
     */
    public static void renderJson(HttpServletResponse response, ResponseModel data)throws IOException {
        response.setContentType("application/json;charset=UTF-8");
        response.setHeader("Access-Control-Allow-Origin", "*");
        ServletOutputStream out = response.getOutputStream();
        String str = new ObjectMapper().writeValueAsString(data);
        out.write(str.getBytes("UTF-8"));
        out.flush();
        out.close();
    }
}
